/*
 ********************************************************************************
 *
 * Copyright (c) 2019-2024 Renesas Electronics Corporation and/or its affiliates
 *
 ********************************************************************************
 */

package com.dialog.suotalib.suota;

public class HeaderInfo68x extends HeaderInfo {

    /*
    typedef struct {
        uint8_t signature[2];
        uint16_t flags;
        uint32_t code_size;
        uint32_t crc;
        uint8_t version[16];
        uint32_t timestamp;
        uint32_t exec_location;
    } __attribute__((packed)) suota_1_1_image_header_t;

    #define SUOTA_1_1_IMAGE_HEADER_SIGNATURE_B1     0x70
    #define SUOTA_1_1_IMAGE_HEADER_SIGNATURE_B2     0x61
    */

    public static final String TYPE = "68x";
    public static final int SIGNATURE = 0x7061;
    public static final int HEADER_SIZE = 36;

    private static final int OFFSET_FLAGS = 2;
    private static final int OFFSET_PAYLOAD_SIZE = 4;
    private static final int OFFSET_PAYLOAD_CRC = 8;
    private static final int OFFSET_VERSION = 12;
    private static final int VERSION_LENGTH = 16;
    private static final int OFFSET_TIMESTAMP = 28;
    private static final int OFFSET_EXEC_LOCATION = 32;

    private int flags;
    private long execLocation;

    public HeaderInfo68x(byte[] header, long totalBytes) {
        super(header, totalBytes);
    }

    public HeaderInfo68x(byte[] raw) {
        super(raw);
    }

    @Override
    protected void initializeTypeSpecific() {
        flags = buffer.getShort(OFFSET_FLAGS) & 0xffff;
        execLocation = buffer.getInt(OFFSET_EXEC_LOCATION) & 0xffffffffL;
    }

    @Override
    public String getType() {
        return TYPE;
    }

    @Override
    public int getSignature() {
        return SIGNATURE;
    }

    @Override
    public int getHeaderSize() {
        return HEADER_SIZE;
    }

    @Override
    protected int getOffsetPayloadSize() {
        return OFFSET_PAYLOAD_SIZE;
    }

    @Override
    protected int getOffsetPayloadCrc() {
        return OFFSET_PAYLOAD_CRC;
    }

    @Override
    protected int getOffsetVersion() {
        return OFFSET_VERSION;
    }

    @Override
    protected int getVersionLength() {
        return VERSION_LENGTH;
    }

    @Override
    protected int getOffsetTimestamp() {
        return OFFSET_TIMESTAMP;
    }

    @Override
    public long getPayloadOffset() {
        return execLocation;
    }

    public int getFlags() {
        return flags;
    }

    public long getExecLocation() {
        return execLocation;
    }
}
